import { setRequestRenderMode } from './BQGIS-manuallyRender'
export const defaultOption = {
    fog: {
        enabled: false,
        maxHeight: 20000,
        fogByDistance: new Cesium.Cartesian4(100, 0, 9000, 0.9),
        color: "#ffffff"
    },
    rain: {
        enabled: false,
        speed: 10,
        size: 20,
        direction: -30
    },
    snow: {
        enabled: false,
        speed: 20
    },
    snowCover: {
        enabled: false,
        maxHeight: 9000, // 大于此高度后不显示
        alpha: 0.6
    }
}



export const BQGIS_weather = {
    /**
     * 雾 option(可选)
     * 
     * @param enabled 是否启用,
     * @param fogByDistance: 浓度
     * @param color: 雾颜色, 
     * @param maxHeight: 最大高度
     * 
     */
    getFogEffect(viewer, option = defaultOption.fog) {
        function setFogEnabled() {
            if (viewer.effects.fog && viewer.fog.maxHeight < viewer.scene.camera.positionCartographic.height) {
                viewer.effects.fog.remove(false)
            } else if (!viewer.effects.fog && viewer.fog.maxHeight > viewer.scene.camera.positionCartographic.height) {
                viewer.fog.addTo(viewer)
            }
        }

        if (!viewer.fog) {
            const obj = {
                ...option
            }
            obj.color = Cesium.Color.fromCssColorString(option.color)
            viewer.fog = new mars3d.effect.FogEffect(obj)
            viewer.addEffect(viewer.fog)
            // 因为 maxHeight 参数存在bug，所以检查高度来判断是否加入雾特效
            if (viewer.fog.maxHeight < viewer.scene.camera.positionCartographic.height) {
                viewer.effects.fog.remove(false)
            }
            viewer.camera.changed.addEventListener(setFogEnabled)
        }
        return new Proxy(viewer.fog, {
            set(obj, key, value) {
                // if (viewer.scene.requestRenderMode) {
                //     viewer.scene.requestRender();
                // }
                if (key === 'maxHeight') {
                    setFogEnabled()
                }
                if (key === 'color') {
                    obj[key] = Cesium.Color.fromCssColorString(value)
                } else {
                    obj[key] = value
                }
                return true
            },
            get(obj, key) {
                if (key === 'color') {
                    return obj[key].toCssHexString()
                } else {
                    return obj[key]
                }
            }
        })
    },
    /**
     * 雨 option(可选)
     * 
     * @param enabled 是否启用,
     * @param speed:  粒子速度
     * @param size:  粒子大小
     * @param direction:  粒子方向
     * 
     */
    getRainEffect(viewer, option = defaultOption.rain) {
        if (!viewer.effects.rain) {
            viewer.addEffect(new mars3d.effect.RainEffect(option))
        }
        return viewer.effects.rain
        // return new Proxy(viewer.effects.rain, {
        //     set(obj, key, value) {
        //         obj[key] = value
        //         if (key === 'enabled') {
        //             setRequestRenderMode(!value)
        //         }
        //         if (viewer.scene.requestRenderMode) {
        //             viewer.scene.requestRender();
        //         }
        //         return true
        //     }
        // })
    },
    /**
     * 雪 option(可选)
     * 
     * @param enabled 是否启用,
     * @param speed:  粒子速度
     * 
     */
    getSnowEffect(viewer, option = defaultOption.snow) {
        if (!viewer.effects.snow) {
            viewer.addEffect(new mars3d.effect.SnowEffect(option))
        }
        return viewer.effects.snow
        return new Proxy(viewer.effects.snow, {
            set(obj, key, value) {
                obj[key] = value
                if (key === 'enabled') {
                    setRequestRenderMode(!value)
                }
                if (viewer.scene.requestRenderMode) {
                    viewer.scene.requestRender();
                }
                return true
            }
        })
    },
    /**
     * 积雪 option(可选)
     * 
     * @param enabled 是否启用,
     * @param alpha:  积雪强度
     * 
     */
    getSnowCover(viewer, option = defaultOption.snowCover) {
        if (!viewer.effects.snowCover) {
            viewer.addEffect(new mars3d.effect.SnowCoverEffect(option))
        }
        return viewer.effects.snowCover
        // return new Proxy(viewer.effects.snowCover, {
        //     set(obj, key, value) {
        //         obj[key] = value
        //         if (viewer.scene.requestRenderMode) {
        //             viewer.scene.requestRender();
        //         }
        //         return true
        //     }
        // })
    }
}

export function setEnvironment() {
    // 大气层外光圈
    map.scene.skyAtmosphere.hueShift = -0.8
    map.scene.skyAtmosphere.saturationShift = -0.7
    map.scene.skyAtmosphere.brightnessShift = -0.33
    // 雾化效果
    map.scene.fog.enabled = true
    map.scene.fog.density = 0.001
    map.scene.fog.minimumBrightness = 0.8
}

export function resetEnvironment() {
    // 大气层外光圈
    map.scene.skyAtmosphere.hueShift = 0.0
    map.scene.skyAtmosphere.saturationShift = 0.0
    map.scene.skyAtmosphere.brightnessShift = 0.0
    // 雾化效果
    map.scene.fog.enabled = false
    map.scene.fog.density = 2.0e-4
    map.scene.fog.minimumBrightness = 0.03
}